import request from '@/utils/request'

export interface QuestionCategory {
  id: number
  name: string
  code: string
  subjectId: number
  subjectName: string
  status: 'ACTIVE' | 'INACTIVE'
  description?: string
  createdAt: string
  updatedAt: string
}

export interface CategoryQuery {
  page?: number
  size?: number
  name?: string
  code?: string
  subjectId?: number
  status?: string
}

export interface PageResponse<T> {
  content: T[]
  totalElements: number
  totalPages: number
  number: number
  size: number
}

export const questionCategoryApi = {
  /**
   * 获取分类列表
   */
  getCategories: (params: CategoryQuery) =>
    request.get<PageResponse<QuestionCategory>>('/question-categories', { params }),

  /**
   * 获取分类详情
   */
  getCategory: (id: number) =>
    request.get<QuestionCategory>(`/question-categories/${id}`),

  /**
   * 创建分类
   */
  createCategory: (data: Omit<QuestionCategory, 'id' | 'createdAt' | 'updatedAt' | 'subjectName'>) =>
    request.post<QuestionCategory>('/question-categories', data),

  /**
   * 更新分类
   */
  updateCategory: (id: number, data: Partial<QuestionCategory>) =>
    request.put<QuestionCategory>(`/question-categories/${id}`, data),

  /**
   * 删除分类
   */
  deleteCategory: (id: number) =>
    request.delete(`/question-categories/${id}`),

  /**
   * 根据学科获取激活的分类列表
   */
  getActiveCategories: (subjectId?: number) =>
    request.get<QuestionCategory[]>('/question-categories/active', {
      params: subjectId ? { subjectId } : {}
    })
}
